<blockquote class="layui-elem-quote">
    <div class="layui-inline">
        <i class="layui-icon layui-icon-list layui-head-icon"></i>
        <span class="layui-font-16 layui-font-green">店铺基础配置</span>
    </div>
</blockquote>
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form">
            <input type="hidden" name="id" value="{$row.id|default=''}">
            <div class="layui-form-item">
                <label class="layui-form-label">积分赠送比例</label>
                <div class="layui-input-inline">
                    <input type="text" name="give_integral_scale" value="{$row.give_integral_scale|default=0}" lay-verify="required" placeholder=""
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux layui-font-red">%</div>


            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否允许用户自定义充值</label>
                <div class="layui-input-block">
                    <input type="radio" name="is_custom_recharge" {notempty name='row.is_custom_recharge'}checked {/notempty} value="1"  title="允许">
                    <input type="radio" name="is_custom_recharge" {empty name='row.is_custom_recharge'}checked {/empty} value="0" title="不允许">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否开启用户签到</label>
                <div class="layui-input-block">
                    <input type="radio" name="is_sign_in" {notempty name='row.is_sign_in'}checked {/notempty} value="1"  title="开启">
                    <input type="radio" name="is_sign_in" {empty name='row.is_sign_in'}checked {/empty} value="0" title="关闭">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">签到奖励配置</label>
                <div class="layui-input-block" style="width: 800px">
                    <table class="layui-table" id="sign-table">
                        <tr>
                            <th>连续签到天数</th>
                            <th>积分奖励</th>
                            <th>操作</th>
                        </tr>
                        {notempty name='row.sign_config'}
                            {foreach $row.sign_config as $vo}
                                <tr>
                                    <td>
                                        <div class="layui-inline">
                                            <input type="number" value="{$vo.sign_in_day}" class="layui-input sign_in_day">
                                        </div>
                                        <span class="layui-font-red">天</span>

                                    </td>
                                    <td>
                                        <div class="layui-inline">
                                            <input type="number" value="{$vo.give_integral}"  class="layui-input award give_integral">
                                        </div>
                                        <span class="layui-font-red">个</span>
                                    </td>
                                    <td>
                                        <a class="layui-btn layui-btn-primary layui-border-red layui-btn-sm del-tr">删除</a>
                                    </td>
                                </tr>
                            {/foreach}
                        {else/}
                            <tr>
                                <td>
                                    <div class="layui-inline">
                                        <input type="number" value="0" class="layui-input sign_in_day">
                                    </div>
                                    <span class="layui-font-red">天</span>

                                </td>
                                <td>
                                    <div class="layui-inline">
                                        <input type="number" value="0"  class="layui-input award give_integral">
                                    </div>
                                    <span class="layui-font-red">个</span>
                                </td>
                                <td>
                                    <a class="layui-btn layui-btn-primary layui-border-red layui-btn-sm del-tr">删除</a>
                                </td>
                            </tr>
                        {/notempty}
                    </table>
                    <div style="text-align: center">
                        <a id="add-tr" class="layui-btn layui-btn-primary layui-border-green layui-btn-sm">继续添加</a>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="baseForm">保存配置</button>
                </div>
            </div>
        </form>

    </div>
</div>
<script type="text/javascript">
    layui.use(['form'], function () {
        var form = layui.form;

        $('#add-tr').click(function (){
            let tpl = '<tr>' +
                '                            <td>' +
                '                                <div class="layui-inline">' +
                '                                    <input type="number" value="{$row.sign_in_day|default=0}" class="layui-input sign_in_day">' +
                '                                </div>' +
                '                                <span class="layui-font-red">天</span>' +
                '                            </td>' +
                '                            <td>' +
                '                                <div class="layui-inline">' +
                '                                    <input type="number" value="{$row.give_integral|default=0}"  class="layui-input award give_integral">' +
                '                                </div>' +
                '                                <span class="layui-font-red">个</span>' +
                '                            </td>' +
                '                            <td>' +
                '                                <a class="layui-btn layui-btn-primary layui-border-red layui-btn-sm del-tr">删除</a>' +
                '                            </td>' +
                '                        </tr>';
            $('#sign-table').append(tpl);
        })

        $('#sign-table').on('click','.del-tr',function (){
            $(this).parents('tr').remove();
        });

        function getSignRule(){
           let tr =  $('#sign-table tr:not(:eq(0))');
           let sign_rule= [];
           tr.each(function (){
               sign_rule.push({
                   sign_in_day: $(this).find('.sign_in_day').val(),
                   give_integral: $(this).find('.give_integral').val()
               });
           })
            return sign_rule;
        }

        form.on('submit(baseForm)', function (data) {
            let sign_rule = getSignRule();
            data.field.sign_config = JSON.stringify(sign_rule);
            $.post('/store/setting/base', data.field, function (res) {
                if (res.code == 1) {
                    layer.msg(res.msg,{icon:1});
                    return false;
                }
                return layer.msg(res.msg, {icon: 2});
            });

            return false;
        });


    });
</script>